/**
 * 选择用户树窗口
 */
Ext.namespace("Ext.ux.calendar");
 
Ext.ux.calendar.SelectUserWindow = Ext.extend(Ext.Window,{

	layout: 'fit',
	width: 500,
	height: 400,
	closeAction: 'hide',
	title: '选择用户',
	plain: true,
	modal: true,
	border: false,
	
	// 获取数据的URL
	url: '',
	parentPanel: '',	//调用它的父面板,
	singleSelect: false,
	
	// 组件初始化
	initComponent: function(){
		
		// 调用父类初始化
		Ext.ux.calendar.SelectUserWindow.superclass.initComponent.call(this);
		this.addEvents('selectUsers');
		
		var tree = new Ext.tree.TreePanel({
			dataUrl: this.url,
	        useArrows: true,
	        autoScroll: true,
	        animate: true,
	        containerScroll: true,
	        border: true,
	        rootVisible: false,
			bodyStyle: 'padding:3px',
	        root: {
	            nodeType: 'async'
	        },
	        tbar: [new Ext.form.TextField({
				width: 200,
				emptyText:'请输入用户的姓名或拼音首字母',
                enableKeyEvents: true,
				listeners:{
					render: function(f){
	                	this.filter = new QM.ux.TreeFilter(tree,{
							clearAction : 'expand'
						});//初始化TreeFilter 
					},
           			keyup: {//添加键盘点击监听
               			 fn:function(t,e){
                  			t.filter.filter(t.getValue());
               		},
               		
                	buffer: 350
           	 }
			}
			}),'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: function() {
					tree.root.reload();;	
				}
			},'-',{
				text: '全部折叠',
				iconCls: 'icon-collapse-all',
				handler: function() {
					tree.collapseAll();;	
				}
			},'-',{
				text: '全部展开',
				iconCls: 'icon-expand-all',
				handler: function() {
					tree.expandAll();
				}
			}],
			buttons:[
				{
				text: '&nbsp;&nbsp;确定',
				width: 60,
				handler: this.selectUser,
				scope: this
			},{
				text: '&nbsp;&nbsp;取消',
				width: 60,
				scope: this,
				handler: function(){
					this.hide();
				}
			}
			]
	    });
	    
	    //保证单选
	    var keepSingleChecked = function(tree, node){
	    	var checkedArray = tree.getChecked();
	    	if(checkedArray.length > 1){
	    		for(var i = 0; i < checkedArray.length; i++){
	    			if(node != checkedArray[i]){
	    				checkedArray[i].getUI().toggleCheck(false);
	    			}
	    		}
	    	}
	    };
	    
	    if(this.singleSelect){
	    	 tree.on('checkchange', function(node, checked){
				keepSingleChecked(tree, node)
			}, this);
	    }
	   

	    this.add(tree);
	    this.tree = tree;
		tree.getRootNode().expand(true);
		
		
	},
	//选择用户
	selectUser: function(){
		this.fireEvent('selectUsers', this.tree.getChecked());
		this.hide();
	}
});
